package com.gggd.aopdemo.aop;

import com.gggd.aopdemo.util.WebApiResult;
import com.gggd.aopdemo.vo.User;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

/**
 * @Description
 * @Author srx
 * @date 2021/12/24 22:32
 */
@Slf4j
@Aspect
@Component
public class AfterReturningDemo {

    @Pointcut("@annotation(com.gggd.aopdemo.anno.MyAnnotation)")
    public void pointCut() {}

    /**
     * 在上面定义的切面方法返回后执行该方法，可以捕获返回对象或者对返回对象进行增强
     * @param joinPoint joinPoint
     * @param result result
     */
    @AfterReturning(pointcut = "pointCut()", returning = "result")
    public void doAfterReturning(JoinPoint joinPoint, Object result) {

        Signature signature = joinPoint.getSignature();
        String classMethod = signature.getName();
        log.info("方法{}执行完毕，返回参数为：{}", classMethod, result);
        // 实际项目中可以根据业务做具体的返回值增强
        log.info("对返回参数进行业务上的增强：{}", result + "增强版");
        //获取方法返回体
        WebApiResult webApiResult = (WebApiResult) result;
        //获取需要脱敏的数据
        User u = (User) webApiResult.getData();
        u.setName("李四");
    }
}
